package app.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import app.config.Config;
import app.dao.AccountDao;
import app.dao.AdminDao;
import app.dao.UserDao;
import app.dao.WorkerDao;
import app.domain.Account;
import app.domain.Admin;
import app.domain.User;
import app.domain.UserInfo;
import app.domain.Worker;
import app.service.UserInfoService;


@Service
public class UserInfoServiceImpl implements UserInfoService {
	
    @Autowired
    private UserDao userDao;
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private WorkerDao workerDao;
	@Override
	public UserInfo getUserInfo(String accountName) {
		UserInfo userInfo=new UserInfo();
		Account account=accountDao.findByAccount(accountName);
		if(account==null)
			return null;
		else{
			userInfo.setAccountId(account.getId());
			userInfo.setAccountName(account.getAccountName());
			userInfo.setPassword(account.getPassword());
			userInfo.setRole(account.getRole());
			switch(account.getRole()){
			   case Config.ROLE_ADMIN:
				   Admin admin=adminDao.getById(account.getId());
				   userInfo.setName(admin.getName());
				   userInfo.setPhone(admin.getPhone());
				   break;
			   case Config.ROLE_USER:
				   User user=userDao.getUser(account.getId());
				   userInfo.setName(user.getName());
				   userInfo.setPhone(user.getPhone());
				   userInfo.setAddress(user.getAddress());
				   userInfo.setImgPath(user.getImgPath());
				   userInfo.setSex(user.getSex());
				   break;
			   case Config.ROLE_WORKER:
				   Worker worker=workerDao.getById(account.getId());
				   userInfo.setName(worker.getName());
				   userInfo.setPhone(worker.getTel());
				   userInfo.setSkill(worker.getSkill());
				   userInfo.setSex(worker.getSex());
				   break;
			}
		}
		return userInfo;
	}
	@Override
	public void updateUserInfo(UserInfo userInfo) {
		// TODO Auto-generated method stub
		
	}
	@Transactional
	@Override
	public void addUserInfo(UserInfo userInfo) {
		 Account account=new Account(userInfo.getAccountName(), userInfo.getPassword(),Config.ROLE_USER);
	     accountDao.addAccount(account);
		 int accountId=account.getId();
	     
		 User user=new User();
	     user.setAccountId(accountId);
	     user.setName(userInfo.getName());
	     user.setPhone(userInfo.getPhone());
	     user.setAddress(userInfo.getAddress());
	     user.setSex(userInfo.getSex());
	     user.setImgPath(userInfo.getImgPath());
	     userDao.addUser(user);
	}
	
    
	
	
}